package br.com.controlecompetenciaweb.model.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Conexao {

	static {
		try {
			// Carregando classe que será utilizada como conector
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private static Connection connection;

	public static Connection getConnection() {
		if (connection == null) {
			try {
				// Tentando realizar conexão
				connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/gerenciamentodecompetencias","root","esee200690");
				System.out.println("Conexao ok.");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return connection;
	}

	public void fecharConexao() {
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// Testando conexão!
		Conexao.getConnection();
	}
}
